from enum import Enum
from datetime import datetime,timedelta


class TaskStatus(Enum):
    PENDING = "未完成"
    COMPLETED = "已完成"
    EXPIRED = "已过期"


class TaskPriority(Enum):
    LOW = "低"
    MEDIUM = "中"
    HIGH = "高"
    URGENT = "紧急"


class Task:
    def __init__(self, title, description, due_date, interval_days=1, priority=TaskPriority.MEDIUM):
        self.id = None
        self.title = title
        self.description = description
        self.due_date = due_date
        self.interval_days = interval_days
        self.priority = priority
        self.status = TaskStatus.PENDING
        self.created_at = datetime.now()
        self.completed_at = None
        self.attachments = []
        self.reminders = []
        self.last_reminded = None
        self.is_reminded = False

    def add_attachment(self, file_path):
        self.attachments.append(file_path)

    def calculate_reminders(self):
        """计算所有提醒日期"""
        if not self.due_date:
            return []

        reminders = []
        current_date = datetime.now().date()
        due_date = self.due_date

        # 确保提醒日期在截止日期之前
        if current_date >= due_date:
            reminders.append(current_date)
            return reminders

        # 计算间隔天数
        days_before = (due_date - current_date).days
        num_reminders = min(days_before, 5)  # 最多提醒5次

        # 生成提醒日期
        for i in range(num_reminders, 0, -1):
            reminder_date = due_date - timedelta(days=i * self.interval_days)
            if reminder_date >= current_date:
                reminders.append(reminder_date)

        return reminders

    def is_due_today(self):
        """检查任务是否今天截止"""
        return self.due_date == datetime.now().date()

    def is_overdue(self):
        """检查任务是否过期"""
        return self.due_date < datetime.now().date() and self.status != TaskStatus.COMPLETED

    def mark_completed(self):
        """标记任务为已完成"""
        self.status = TaskStatus.COMPLETED
        self.completed_at = datetime.now()

    def mark_expired(self):
        """标记任务为已过期"""
        if self.is_overdue():
            self.status = TaskStatus.EXPIRED

    def mark_reminded(self):
        """标记任务为已提醒"""
        self.is_reminded = True
        self.last_reminded = datetime.now()